package com.example.xijinping.nettest.Adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xijinping.nettest.CarBean;
import com.example.xijinping.nettest.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.BitmapCallback;

import java.util.ArrayList;

import okhttp3.Call;

/**
 * Created by xijinping on 2016/11/15.
 */
public class MyAdapter extends BaseAdapter {

    ArrayList<CarBean.Car> data;
    Context context;

    public MyAdapter(ArrayList<CarBean.Car> data, Context context) {
        super();
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //初始化holder对象
        final ViewHold holder;
        if(convertView == null){
            //把条目布局转化为view对象
            convertView = View.inflate(context,R.layout.activity_item, null);
            //初始化holder对象，并初始化holder中的控件
            holder = new ViewHold();
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.tv_date = (TextView) convertView.findViewById(R.id.tv_date);
//            holder.iv = (SmartImageView) convertView.findViewById(R.id.iv);
            holder.iv = (ImageView) convertView.findViewById(R.id.iv);

            //给当前view做个标记，并把数据存到该tag中
            convertView.setTag(holder);

        }else {
            //如果当前view存在，则直接从中取出其保存的控件及数据
            holder = (ViewHold) convertView.getTag();
        }
        //通过position获取当前item的car数据，从car数据中取出title、pubDate和image
        CarBean.Car car = data.get(position);
        holder.tv_title.setText(car.title);
        holder.tv_date.setText(car.pubDate);

        //使用SmartImageView的setImageUrl方法下载图片
//        holder.iv.setImageUrl(car.image);
//        holder.iv.setImageURI(Uri.parse(car.image));
        OkHttpUtils
                .get()//
                .url(car.image)//
                .tag(this)//
                .build()//
                .connTimeOut(20000)
                .readTimeOut(20000)
                .writeTimeOut(20000)
                .execute(new BitmapCallback()
                {
                    @Override
                    public void onError(Call call, Exception e, int id)
                    {
//                        mTv.setText("onError:" + e.getMessage());
                    }

                    @Override
                    public void onResponse(Bitmap bitmap, int id)
                    {
                        Log.e("TAG", "onResponse：complete");
                        holder.iv.setImageBitmap(bitmap);
                    }
                });

        return convertView;
    }

    /*
     * 用来存放item布局中控件的holder类
     */
    class ViewHold{
        ImageView iv; //显示图片的控件，注意是SmartImageView
        TextView tv_title; //显示标题的控件
        TextView tv_date;  //显示日期的控件
    }
}
